package system

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/system"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

var SysOauthUser = cSystemSys_oauth_user{}

type cSystemSys_oauth_user struct{}

// GetSysOauthUserList 获取列表
func (u *cSystemSys_oauth_user) GetSysOauthUserList(ctx context.Context, req *system.GetSysOauthUserListReq) (res *system.GetSysOauthUserListRes, err error) {
	var reqData = new(model.GetSysOauthUserListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.SystemSysOauthUser().GetSysOauthUserList(ctx, reqData)
	res = new(system.GetSysOauthUserListRes)
	gconv.Scan(dataList, &res.Data)
	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// AddSysOauthUser 添加数据
func (u *cSystemSys_oauth_user) AddSysOauthUser(ctx context.Context, req *system.AddSysOauthUserReq) (res *system.AddSysOauthUserRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.SysOauthUserAddInput{}
	gconv.Scan(req, &data)
	// data.CreatedBy = userInfo.Id
	err = service.SystemSysOauthUser().AddSysOauthUser(ctx, data)
	return
}

// EditSysOauthUser 修改数据
func (u *cSystemSys_oauth_user) EditSysOauthUser(ctx context.Context, req *system.EditSysOauthUserReq) (res *system.EditSysOauthUserRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.SysOauthUserEditInput{}
	gconv.Scan(req, &data)
	// data.UpdateBy = userInfo.Id //如果需要保存信息，把这个打开
	err = service.SystemSysOauthUser().EditSysOauthUser(ctx, data)
	return
}

// DeleteSysOauthUser 删除数据
func (u *cSystemSys_oauth_user) DeleteSysOauthUser(ctx context.Context, req *system.DeleteSysOauthUserReq) (res *system.DeleteSysOauthUserRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.SystemSysOauthUser().DeleteSysOauthUser(ctx, req.Ids)
	return
}
